// utils/request.js

const BASE_URL = ''

class Request {
	constructor() {
		// this.interceptors = {
		// 	request: null,
		// 	response: null
		// }
	}

	// // 请求拦截器
	// interceptorsRequest(fn) {
	// 	this.interceptors.request = fn
	// }

	// // 响应拦截器
	// interceptorsResponse(fn) {
	// 	this.interceptors.response = fn
	// }

	request(options) {
		// 合并配置
		const config = {
			data: options.data || {},
			method: options.method || 'GET',
			header: {
				'Content-Type': 'application/json',
				...this.getAuthHeader(),
				...options.header
			},
			...options,
			url: BASE_URL + options.url,
		}

		// 请求拦截
		// if (this.interceptors.request) {
		// 	const interceptorResult = this.interceptors.request(config)
		// 	if (interceptorResult === false) {
		// 		return Promise.reject(new Error('请求被拦截'))
		// 	}
		// }

		return new Promise((resolve, reject) => {
			uni.request({
				...config,
				success: (response) => {
					// 响应拦截
					// if (this.interceptors.response) {
					// 	const interceptorResult = this.interceptors.response(response)
					// 	if (interceptorResult !== undefined) {
					// 		resolve(interceptorResult)
					// 		return
					// 	}
					// }

					// 统一处理HTTP状态码
					if (response.statusCode >= 200 && response.statusCode < 300) {
						resolve(response.data)
					} else {
						this.handleError(response, reject)
					}
				},
				fail: (error) => {
					this.handleError(error, reject)
				}
			})
		})
	}

	// 获取认证头信息
	getAuthHeader() {
		const token = uni.getStorageSync('token');
		return { token: token }
	}

	// 统一错误处理
	handleError(error, reject) {
		let errorMessage = '网络请求失败'

		if (error.statusCode === 401) {
			errorMessage = '未授权，请重新登录'
			this.handleUnauthorized()
		} else if (error.statusCode === 404) {
			errorMessage = '请求资源不存在'
		} else if (error.statusCode >= 500) {
			errorMessage = '服务器错误'
		}

		uni.showToast({
			title: errorMessage,
			icon: 'none'
		})

		reject(error)
	}

	// 处理未授权
	handleUnauthorized() {
		// 清除token
		uni.removeStorageSync('token')
		// 跳转到登录页
		uni.navigateTo({
			url: '/pages/account/login'
		})
	}

	// 快捷方法
	get(url, data = {}, options = {}) {
		return this.request({
			url,
			data,
			method: 'GET',
			...options
		})
	}

	post(url, data = {}, options = {}) {
		return this.request({
			url,
			data,
			method: 'POST',
			...options
		})
	}

	put(url, data = {}, options = {}) {
		return this.request({
			url,
			data,
			method: 'PUT',
			...options
		})
	}

	delete(url, data = {}, options = {}) {
		return this.request({
			url,
			data,
			method: 'DELETE',
			...options
		})
	}

	// 上传文件方法
	upload(url, filePath, data = {}, options = {}) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: BASE_URL + url,
				filePath: filePath,
				name: 'file',
				formData: data,
				header: {
					...this.getAuthHeader(),
					...options.header
				},
				success: (response) => {
					try {
						const responseData = JSON.parse(response.data)
						if (response.statusCode >= 200 && response.statusCode < 300) {
							resolve(responseData)
						} else {
							this.handleError(response, reject)
						}
					} catch (error) {
						// 处理非JSON响应
						if (response.statusCode >= 200 && response.statusCode < 300) {
							resolve(response.data)
						} else {
							this.handleError(response, reject)
						}
					}
				},
				fail: (error) => {
					this.handleError(error, reject)
				}
			})
		})
	}
}

export default new Request()